package com.hfy.admin.listener;

import com.alibaba.fastjson.JSON;
import com.hfy.model.Bo.SendCaptchaMailBo;
import com.rabbitmq.client.Channel;
import constants.RabbitConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @ author 韩
 * time: 2024/12/18 17:37
 */

@Component
@Slf4j
public class MQListener {

//    @Bean
//    public JavaMailSender mailSender() {
//        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//        // 配置其他属性
//        mailSender.getJavaMailProperties().put("mail.smtp.auth", "true");
//        mailSender.getJavaMailProperties().put("mail.smtp.starttls.enable", "true");
//        mailSender.getJavaMailProperties().put("mail.debug", "true");
//        mailSender.getJavaMailProperties().put("mail.smtp.socketFactoryClass", "javax.net.ssl.SSLSocketFactory");
//        return mailSender;
//    }

    @Autowired
    private JavaMailSender mailSender;

    @RabbitListener(queues = { RabbitConstants.COMMON_SEND_CAPTCHA_MAIL_QUEUE })
    public void listenSendCaptchaQueue(String msg, Message message, Channel channel) {
        SendCaptchaMailBo bo = JSON.parseObject(msg, SendCaptchaMailBo.class);

        String captcha = bo.getCaptcha();
        String email = bo.getEmail();



        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setTo(email);
            helper.setSubject("您的验证码");
            helper.setFrom("3168134942@qq.com");

            // HTML content
            String htmlContent = "<!DOCTYPE html>" +
                    "<html lang='zh-CN'>" +
                    "<head>" +
                    "<meta charset='UTF-8'>" +
                    "<meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                    "<title>验证码</title>" +
                    "<style>" +
                    "body { font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0; }" +
                    ".email-container { max-width: 600px; margin: 0 auto; background-color: #ffffff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }" +
                    ".header { text-align: center; padding: 10px 0; background-color: #333333; color: #ffffff; border-radius: 8px 8px 0 0; }" +
                    ".content { padding: 20px; text-align: center; }" +
                    ".verification-code { font-size: 24px; font-weight: bold; color: #333; margin: 20px 0; }" +
                    ".footer { text-align: center; font-size: 12px; color: #777; padding: 10px 0; border-top: 1px solid #ddd; }" +
                    "</style>" +
                    "</head>" +
                    "<body>" +
                    "<div class='email-container'>" +
                    "<div class='header'><h1>验证码</h1></div>" +
                    "<div class='content'>" +
                    "<p>亲爱的用户，</p>" +
                    "<p>您的验证码是：</p>" +
                    "<div class='verification-code'>" + captcha + "</div>" +
                    "<p>请使用此验证码完成登录过程。</p>" +
                    "</div>" +
                    "<div class='footer'><p>如果您没有请求此验证码，请忽略此邮件。</p></div>" +
                    "</div>" +
                    "</body>" +
                    "</html>";

            helper.setText(htmlContent, true);

            // Send the email
            mailSender.send(mimeMessage);
            log.info("验证码邮件成功投送到 {}", email);
        } catch (MessagingException e) {
            log.error("验证码邮件未能投送到 {}", email, e);
        }


    }

}
